package com.rede.didiok.rank.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.utils.PageUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.rank.entity.AttrValueEntity;
import com.rede.didiok.rank.service.AttrService;
import com.rede.didiok.rank.service.AttrValueService;
import com.rede.didiok.rank.vo.AttrRespVo;
import com.rede.didiok.rank.vo.AttrVo;


/**
 * 榜单成员的属性表
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-07-20 22:09:24
 */
@RestController
@RequestMapping("rank/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Resource
    private AttrValueService attrValueService;

    /**
     * 获取榜单的属性
     * @param catelogId
     * @return
     */
//    @GetMapping("/listforrank/{rankId}")
//    public R attrListForRank(@PathVariable("rankId") Long rankId){
//
//        List<AttrValueEntity> entities = attrValueService.attrListForRank(rankId);
//
//        return R.ok().put("data",entities);
//    }

    @GetMapping("/list/{catelogId}")
    public R attrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 根据 memberId 查询榜单成员的属性
     * @param memberId
     * @return
     */
    @GetMapping("/listformember/{memberUid}")
    public R listForMember(@PathVariable("memberUid") String memberUid){

        List<AttrValueEntity> entities = attrValueService.listForMember(memberUid);

        return R.ok().put("data",entities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("rank:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据 attrId 查询属性信息
     * @param attrId
     * @return
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        //AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVo);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("rank:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("rank:attr:update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }


    /**
     * 根据榜单成员id修改榜单成员的属性
     * @param memberUid
     * @param entities
     * @return
     */
    @PostMapping("/update/{memberUid}")
    public R updateSpuAttr(@PathVariable("memberUid") String memberUid,
                           @RequestBody List<AttrValueEntity> entities){

        attrValueService.updateRankMemberAttr(memberUid,entities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("rank:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
